import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class eg9_5 {
    public static void main(String[] args) {
        CardLayoutFrame frame = new CardLayoutFrame();
    }

    public static class CardLayoutFrame extends JFrame implements ActionListener {
        private JPanel leftPanel;
        private JPanel rightPanael;
        private CardLayout cardLayout;

        private  JButton nextBUtton;
        private JButton preBUtton;

        public CardLayoutFrame(){
            leftPanel =new JPanel();
            rightPanael  = new JPanel();
            cardLayout = new CardLayout(10,10);
            leftPanel.setLayout(cardLayout);
            JButton button;
            for (int i=1;i<50;i++){
                button = new JButton("按钮"+i);
                leftPanel.add("按钮"+i,button);
            }
            nextBUtton = new JButton("下一个");
            nextBUtton.addActionListener(this);
            preBUtton = new JButton("上一个");
            preBUtton.addActionListener(this);
            rightPanael.setLayout(new GridLayout(2,1,50,50));
            rightPanael.add(nextBUtton);
            rightPanael.add(preBUtton);

            this.setLayout(new GridLayout(1,2));
            this.add(leftPanel);
            this.add(rightPanael);
            this.setTitle("CardLayout卡片式布局");
            this.setSize(600,400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(nextBUtton)){
                cardLayout.next(leftPanel);
            }
            if (e.getSource().equals(preBUtton)){
                cardLayout.previous(leftPanel);
            }

        }
    }
}
